# Build recipe for tqt3 (trinity).
#
# Copyright (c) 2019-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=tqt3
version=20220430_ec04b50a
release=1

# Define a category for the output of the package name
pkgcategory=tde

tarname=${program}-${version}.tar.lz

# Remote source(s)
fetch="
 https://dragora.mirror.garr.it/current/sources/$tarname
 rsync://rsync.dragora.org/current/sources/$tarname
"

description="
Qt is a multiplatform C++ GUI application framework.

Qt 3.3 introduces new features and many improvements over
the 3.2.x series. See the changes file for details.

The Qt version 3.3 series is binary compatible with the
3.2.x series - applications compiled for 3.2 will continue
to run with 3.3.
"

homepage=https://www.trinitydesktop.org/
license="GPLv2+, GPLv3+ | The Q Public License"

# Source documentation
docs="FAQ LICENSE* README*"
docsdir="${docdir}/${program}-${version}"

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Remove unneeded specs for targets other than GNU/Linux
    ( cd mkspecs && find . -maxdepth 1 \( -type d -a ! -name 'linux-g++' \) -exec rm -rf -- '{}' \; )

    # Do not build the HTML documentation in order to save disk space
    #sed -i '/htmldocs/d' src/qt_install.pri

    # Set custom compilation flags from Qi
    sed -i \
     -e "s:QMAKE_CFLAGS_RELEASE.*=:QMAKE_CFLAGS_RELEASE=${QICXXFLAGS} :" \
      mkspecs/linux*/qmake.conf

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    LD_LIBRARY_PATH="${PWD}/lib:${LD_LIBRARY_PATH}"
    LD_LIBRARY_PATH="${LD_LIBRARY_PATH%%:*}";	# Get the rid of ":".
    export LD_LIBRARY_PATH
    echo "New LD_LIBRARY_PATH: $LD_LIBRARY_PATH"

    echo yes | ./configure \
     -L/usr/lib${libSuffix} \
     -I/usr/include/freetype2 \
     -I/usr/include/tirpc \
     -prefix "/usr" \
     -libdir "/usr/lib${libSuffix}" \
     -sysconfdir "/etc/tqt3" \
     -headerdir "/usr/include/tqt3" \
     -plugindir "/usr/lib${libSuffix}/tqt3/plugins" \
     -datadir "/usr/share/tqt3" \
     -translationdir "/usr/share/tqt3/translations" \
     -docdir "$docsdir" \
     -no-exceptions \
     -qt-gif \
     -system-zlib \
     -system-libpng \
     -system-libjpeg \
     -system-libmng \
     -qt-imgfmt-mng \
     -qt-imgfmt-png \
     -qt-imgfmt-jpeg \
     -plugin-imgfmt-mng \
     -thread \
     -shared \
     -fast \
     -glibmainloop \
     -tablet \
     -enable-opengl \
     -dlopen-opengl \
     -nis \
     -sm \
     -xshape \
     -xinerama \
     -xcursor \
     -xrandr \
     -xft \
     -xrender \
     -xkb \
     -release

    make -i -j${jobs} sub-tools
    make -j${jobs} INSTALL_ROOT="$destdir" install

    unset -v LD_LIBRARY_PATH

    # Strip remaining binaries and libraries
    find "$destdir" -type f | xargs file | \
     awk '/ELF/ && /executable/ || /shared object/' | \
      cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}/$docsdir"
    cp -p $docs "${destdir}/$docsdir"
}

